<template>
    <view class="content">
        <image @tap.stop.prevent="konck" class="bg w100pr h100pr" src="/static/images/index/bg.png"></image>
        <view class="oper flex flex-jsb flex-ac">
            <view class="center">
                <view class="title">功德木鱼</view>
                <view class="count">{{ count }}</view>
            </view>
        </view>
        <image @tap.stop.prevent="konck" class="bowl" src="/static/images/index/bowl.png"></image>
        <image :animation="animationData" @tap.stop.prevent="konck" class="stick" src="/static/images/index/stick.png"></image>
        <view :animation="agd1" class="gongde">功德 + 1</view>
        <view :animation="agd2" class="gongde">功德 + 1</view>
        <view :animation="agd3" class="gongde">功德 + 1</view>
        <view :animation="agd4" class="gongde">功德 + 1</view>
        <view :animation="agd5" class="gongde">功德 + 1</view>
        <view :animation="agd6" class="gongde">功德 + 1</view>
        <view :animation="agd7" class="gongde">功德 + 1</view>
        <view :animation="agd8" class="gongde">功德 + 1</view>
        <view :animation="agd9" class="gongde">功德 + 1</view>
        <view :animation="agd0" class="gongde">功德 + 1</view>
        <view class="tool">
            <view @tap="openaudio" class="tool-list">
                <view>
                    <image mode="widthFix" :src="audio ? '/static/images/audio.png' : '/static/images/audio-n.png'"></image>
                </view>
                <view class="text">声音</view>
            </view>
            <view @tap="resetcount" class="tool-list">
                <view>
                    <image mode="widthFix" src="/static/images/reset.png"></image>
                </view>
                <view class="text">重置</view>
            </view>
            <view @tap="autoplay" class="tool-list">
                <view>
                    <image mode="widthFix" :src="auto ? '/static/images/auto.png' : '/static/images/auto-n.png'"></image>
                </view>
                <view class="text">自动</view>
            </view>
        </view>
        <button class="share-btn" hoverClass="none" openType="share">
            <image class="w100pr h100pr" src="/static/images/share-btn.png"></image>
        </button>
    </view>
</template>

<script>
getApp();
var t = '';
var e = '';
export default {
        data() {
            return {
                auto: false,
                count: 0,
                audio: true,
                opengo: true,
                animationData: '',
                n: '',
                agd1: '',
                agd2: '',
                agd3: '',
                agd4: '',
                agd5: '',
                agd6: '',
                agd7: '',
                agd8: '',
                agd9: '',
                agd0: ''
            };
        },
        onShow: function () {
            uni.showShareMenu({
                withShareTicket: false,
                menus: ['shareAppMessage', 'shareTimeline']
            });
            if (uni.getStorageSync('my' + this.getCurrentDate())) {
                console.log(uni.getStorageSync('my' + this.getCurrentDate()));
                this.setData({
                    count: uni.getStorageSync('my' + this.getCurrentDate())
                });
            } else {
                for (let t = -1; t > -3; t--) {
                    uni.removeStorageSync('my' + this.getCurrentDate(t));
                }
            }
        },
        onHide: function () {
            this.innerAudioContext.stop();
            if (this.timer) {
                clearInterval(this.timer);
            }
            this.setData({
                auto: false
            });
        },
        onLoad: function () {
            this.onLoadClone3389();
        },
        onShareAppMessage: function (t) {
            return {
                title: uni.getStorageSync('my' + this.getCurrentDate())
                    ? '今日我已功德+' + uni.getStorageSync('my' + this.getCurrentDate()) + '，你也一起来敲电子木鱼吧！'
                    : '我发现了一款不错的解压神器！快来试一试吧！',
                path: '/pages/index/index',
                imageUrl: '/static/images/index/share.jpg'
            };
        },
        onShareTimeline: function () {
            return {
                title: uni.getStorageSync('my' + this.getCurrentDate())
                    ? '今日我已功德+' + uni.getStorageSync('my' + this.getCurrentDate()) + '，你也一起来敲电子木鱼吧！'
                    : '我发现了一款不错的解压神器！快来试一试吧！',
                imageUrl: '/static/images/index/share.jpg'
            };
        },
        onAddToFavorites: function (t) {
            return {
                title: '功德木鱼',
                imageUrl: '/static/images/index/share.jpg'
            };
        },
        methods: {
            onLoadClone3389: function () {
                var that = this;
                console.log(getApp().globalData.videoid);
                if (getApp().globalData.videoid && uni.createRewardedVideoAd) {
                    (e = uni.createRewardedVideoAd({
                        adUnitId: getApp().globalData.videoid
                    })).onLoad(function () {});
                    e.onError(function (t) {});
                    e.onClose(function (t) {
                        if (t && t.isEnded) {
                            that.overvideo();
                        } else {
                            uni.showToast({
                                title: '完整看完才能解锁哦~',
                                icon: 'none'
                            });
                        }
                    });
                }
                if (getApp().globalData.interid) {
                    if (uni.createInterstitialAd) {
                        (t = uni.createInterstitialAd({
                            adUnitId: getApp().globalData.interid
                        })).onLoad(function () {}),
                            t.onError(function (t) {}),
                            t.onClose(function () {});
                    }
                    setTimeout(function () {
                        if (t) {
                            t.show().catch(function (t) {
                                console.error(t);
                            });
                        }
                    }, 2000);
                }
                this.innerAudioContext = uni.createInnerAudioContext();
                this.innerAudioContext.src = '/static/audio/qiaoji.mp3';
                this.innerAudioContext.onPlay(function () {});
                this.innerAudioContext.onError(function (t) {
                    console.log(t.errMsg);
                    console.log(t.errCode);
                });
                this.innerAudioContext2 = uni.createInnerAudioContext();
                this.innerAudioContext2.src = '/static/audio/qiaoji.mp3';
                this.innerAudioContext2.onPlay(function () {});
                this.innerAudioContext2.onError(function (t) {
                    console.log(t.errMsg);
                    console.log(t.errCode);
                });
                this.innerAudioContext3 = uni.createInnerAudioContext();
                this.innerAudioContext3.src = '/static/audio/qiaoji.mp3';
                this.innerAudioContext3.onPlay(function () {});
                this.innerAudioContext3.onError(function (t) {
                    console.log(t.errMsg);
                    console.log(t.errCode);
                });
                this.innerAudioContext4 = uni.createInnerAudioContext();
                this.innerAudioContext4.src = '/static/audio/qiaoji.mp3';
                this.innerAudioContext4.onPlay(function () {});
                this.innerAudioContext4.onError(function (t) {
                    console.log(t.errMsg);
                    console.log(t.errCode);
                });
                this.aniamtion = uni.createAnimation({
                    duration: 50,
                    transformOrigin: 'right top',
                    timingFunction: 'ease-in-out'
                });
                for (var o = 0; o < 10; o++) {
                    this['agd' + o] = uni.createAnimation({
                        duration: 250,
                        transformOrigin: 'right top',
                        timingFunction: 'ease-out'
                    });
                }
            },

            resetcount: function () {
                var that = this;
                uni.showModal({
                    title: '提示',
                    content: '是否重置数据？',
                    success: function (e) {
                        if (e.confirm) {
                            if (uni.getStorageSync('my' + that.getCurrentDate())) {
                                uni.removeStorageSync('my' + that.getCurrentDate());
                            }
                            that.setData({
                                count: 0
                            });
                        } else {
                            if (e.cancel) {
                                console.log('用户点击取消');
                                uni.reportEvent('refresh', {
                                    yesno: 'no'
                                });
                            }
                        }
                    }
                });
            },

            openaudio: function () {
                this.setData({
                    audio: !this.audio
                });
            },

            overvideo: function () {
                uni.setStorageSync('my-key', this.getCurrentDate());
                this.timer = setInterval(this.konck, 700);
                this.setData({
                    auto: true
                });
            },

            konck: function () {
                if (this.audio) {
                    this.count % 4 == 0
                        ? (this.innerAudioContext4.stop(), this.innerAudioContext4.play())
                        : this.count % 3 == 0
                        ? (this.innerAudioContext3.stop(), this.innerAudioContext3.play())
                        : this.count % 2 == 0
                        ? (this.innerAudioContext2.stop(), this.innerAudioContext2.play())
                        : (this.innerAudioContext.stop(), this.innerAudioContext.play());
                }
                var t = '' + this.count;
                var e = t.substr('' + t.length - 1);
                console.log(e);
                var n = ['agd' + e];
                this['agd' + e].top('51%').step().top('40%').opacity(1).step(50).opacity(0).step().step().top('51%');
                this.aniamtion.rotate(0).step().rotate(40).step();
                this.setData({
                    animationData: this.aniamtion.export(),
                    [n]: this['agd' + e].export()
                });
                this.setData({
                    count: this.count + 1
                });
                uni.setStorageSync('my' + this.getCurrentDate(), this.count);
            },

            autoplay: function () {
                return this.auto
                    ? (this.timer && clearInterval(this.timer),
                      this.setData({
                          auto: false
                      }),
                      false)
                    : uni.getStorageSync('my-key') && uni.getStorageSync('my-key') == this.getCurrentDate()
                    ? (this.overvideo(), false)
                    : void (e
                          ? uni.showModal({
                                title: '未解锁',
                                content: '看一段视频，可在今天无限使用此功能',
                                confirmColor: '#439057',
                                success: function (t) {
                                    if (t.confirm && e) {
                                        e.show().catch(function () {
                                            e.load()
                                                .then(function () {
                                                    return e.show();
                                                })
                                                .catch(function (t) {
                                                    this.timer = setInterval(this.konck, 700);
                                                    this.setData({
                                                        auto: true
                                                    });
                                                });
                                        });
                                    }
                                }
                            })
                          : ((this.timer = setInterval(this.konck, 700)),
                            this.setData({
                                auto: true
                            })));
            },

            getCurrentDate: function (t = 0) {
                const e = new Date();
                e.setDate(e.getDate() + t);
                const n = {
                    year: e.getFullYear(),
                    month: e.getMonth() + 1,
                    date: e.getDate()
                };
                return n.year + '' + (n.month >= 10 ? n.month : '0' + n.month) + (n.date >= 10 ? n.date : '0' + n.date);
            }
        }
};
</script>
<style>
@import './index.css';
</style>
